//
//  QYXAuthorSigleVC.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXAuthorSigleVC.h"
#import "QYXDailyTableViewCell.h"
#import "QYXFindMoreModel.h"
#import "QYXDailyItemListModel.h"
#import "UINavigationBar+Background.h"
#import "QYXDailyDetailViewController.h"
#import "AppDelegate.h"

@interface QYXAuthorSigleVC ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

/** UItableView */
@property (nonatomic, strong) UITableView *tableView;

/** 数据 */
@property (nonatomic, strong) NSMutableArray *data;

/** 数据1 */
@property (nonatomic, strong) NSMutableArray *dataArr;

/** 数据2 */
@property (nonatomic, strong) NSMutableArray *shareDataArr;

/** line1 */
@property (nonatomic, strong) UIView *line1 ;
/** line2 */
@property (nonatomic, strong) UIView *line2 ;

@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;


@end

@implementation QYXAuthorSigleVC
{
    BOOL flag;
}

- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    

    UIImage *image = [[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.data = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    self.shareDataArr = [NSMutableArray array];
    [self creatTableView];
    
    [self requiredData];
    [self requiredData2];
}






#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 64) {
        self.navigationItem.title = _model.name;
        
    } else {
        self.navigationItem.title = @"";
    }
}
#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建TableView

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [self creatTabkeViewHeader];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 240 *KFitHeight;
    self.tableView.sectionFooterHeight = 40 *KFitHeight;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[QYXDailyTableViewCell class] forCellReuseIdentifier:@"QYXDailyTableViewCell"];
    
}

#pragma mark - 创建头视图
- (void)creatTabkeViewHeader
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150 *KFitHeight)];
    UIImageView *pic = [[UIImageView alloc] init];
    [view addSubview:pic];
    
    [pic sd_setImageWithURL:[NSURL URLWithString:_model.icon]];
    
    [pic makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.equalTo(50);
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(view.top).offset(5);
        
        pic.layer.cornerRadius = 25;
        pic.layer.masksToBounds = YES;
        
    }];
    
    UILabel *titleName = [[UILabel alloc] init];
    [view addSubview:titleName];
    titleName.text = _model.name;
    titleName.font= [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:15];
    titleName.textAlignment = NSTextAlignmentCenter;
    titleName.textColor = [UIColor colorWithWhite:0.000 alpha:0.689];
    
    [titleName makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(150);
        make.centerX.equalTo(pic.centerX);
        make.top.equalTo(pic.bottom).offset(14);
        make.height.equalTo(30);
    }];
    
    
    UILabel *desLabel = [[UILabel alloc] init];
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:12];
    desLabel.textColor = [UIColor lightGrayColor];
    desLabel.numberOfLines = 0;
    [view addSubview:desLabel];
    desLabel.text = _model.des;
    
    
    [desLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleName.bottom).offset(15);
        make.bottom.equalTo(view.bottom).offset(-15);
        make.left.equalTo(view.left).offset(50);
        make.right.equalTo(view.right).offset(-50);
    }];
    
    self.tableView.tableHeaderView = view;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 *KFitHeight)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"Lobster 1.4" size:15];
    label.text = @"- The End -";
    self.tableView.tableFooterView = label;
}

#pragma mark - Delegate


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_data.count == 0) {
        return nil;
    }
    
    QYXDailyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYXDailyTableViewCell" forIndexPath:indexPath];
    cell.model = self.data[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYXDailyDetailViewController *detailVC = [QYXDailyDetailViewController new];
    
    detailVC.listDataSource = _data;
    //    detailVC.modelSection = indexPath.section;
    detailVC.modelRow = indexPath.row;
    AppDelegate *tempDelegate = [UIApplication sharedApplication].delegate;
    tempDelegate.tabBar.backView.hidden = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 *KFitHeight)];
    view.backgroundColor = [UIColor whiteColor];
    
    float space = (kScreenWidth - 70 *kFitWidth *2) / 3;
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOne.frame = CGRectMake(space, 5, 70 *kFitWidth, 30);
    [view addSubview:buttonOne];
    [buttonOne setTitle:@"按时间排序" forState:UIControlStateNormal];
    buttonOne.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:14];
    [buttonOne setTintColor:[UIColor colorWithWhite:0.000 alpha:0.787]];
    [buttonOne addTarget:self action:@selector(buttonOne:) forControlEvents:UIControlEventTouchUpInside];
    self.btn1 = buttonOne;
    
    
    self.line1 = [[UIView alloc] initWithFrame:CGRectMake(buttonOne.frame.origin.x, buttonOne.frame.origin.y + buttonOne.frame.size.height + 1, buttonOne.frame.size.width, 1)];
    [view addSubview:_line1];
    _line1.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
    
    self.line2 = [[UIView alloc] initWithFrame:CGRectMake(buttonOne.frame.origin.x, buttonOne.frame.origin.y - 1, buttonOne.frame.size.width, 1)];
    [view addSubview:_line2];
    _line2.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
    [view addSubview:_line2];

    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonTwo.frame = CGRectMake(space * 2 + 80 *kFitWidth, 5, 70 *kFitWidth, 30);
    [view addSubview:buttonTwo];
    [buttonTwo setTitle:@"分享排行榜" forState:UIControlStateNormal];
    buttonTwo.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:14];
    [buttonTwo setTintColor:[UIColor colorWithWhite:0.000 alpha:0.787]];
      [buttonTwo addTarget:self action:@selector(buttonTwo:) forControlEvents:UIControlEventTouchUpInside];
    self.btn2 =buttonTwo;
    
    
    if (flag == 1) {
            _line1.frame = CGRectMake(_btn2.frame.origin.x, _btn2.frame.origin.y + _btn2.frame.size.height + 1, _btn2.frame.size.width, 1);
            _line2.frame = CGRectMake(_btn2.frame.origin.x, _btn2.frame.origin.y - 1, _btn2.frame.size.width, 1);
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 *KFitHeight;
}





#pragma mark - 分区点击Button

- (void)buttonOne:(UIButton *)button
{
    [self.data removeAllObjects];
    [self.data addObjectsFromArray:_dataArr];
    flag = 0;
   
    
    [UIView animateWithDuration:0.5 animations:^{
        _line1.frame = CGRectMake(_btn1.frame.origin.x, _btn1.frame.origin.y + _btn1.frame.size.height + 1, _btn1.frame.size.width, 1);
        _line2.frame = CGRectMake(_btn1.frame.origin.x, _btn1.frame.origin.y - 1, _btn1.frame.size.width, 1);
    } completion:^(BOOL finished) {
        [self.tableView reloadData];

    }];
    
  

}

- (void)buttonTwo:(UIButton *)button
{
    [self.data removeAllObjects];
    [self.data addObjectsFromArray:_shareDataArr];
    flag = 1;

    [UIView animateWithDuration:0.5 animations:^{
        _line1.frame = CGRectMake(_btn2.frame.origin.x, _btn2.frame.origin.y + _btn2.frame.size.height + 1, _btn2.frame.size.width, 1);
        _line2.frame = CGRectMake(_btn2.frame.origin.x, _btn2.frame.origin.y - 1, _btn2.frame.size.width, 1);
    } completion:^(BOOL finished) {
        [self.tableView reloadData];

    }];

}
#pragma mark - 网络请求

- (void)requiredData
{
    
    [QYXNetTool getNetWithUrl:[NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v3/pgc/videos?_s=f35d723e232d6c4685cb380068783257&f=iphone&net=wifi&num=20&pgcId=%ld&start=0&strategy=date&u=ebe4ca1f9d83e4dc4f915b0495e6e1946ee0eb93&v=2.2.0&vc=721", _model.id] urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        for (NSDictionary *d in result[@"itemList"]) {
            QYXDailyItemListModel *model = [[QYXDailyItemListModel alloc] initWithDic:d[@"data"]];
            [self.dataArr addObject:model];
        }
//        NSLog(@"------- %@", _dataArr);
        if (flag == 0) {
            [self buttonOne:self.btn1];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
    }];
}


- (void)requiredData2
{
    
    [QYXNetTool getNetWithUrl:[NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v3/pgc/videos?_s=f35d723e232d6c4685cb380068783257&f=iphone&net=wifi&num=20&pgcId=%ld&start=0&strategy=shareCount&u=ebe4ca1f9d83e4dc4f915b0495e6e1946ee0eb93&v=2.2.0&vc=721", _model.id] urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        for (NSDictionary *d in result[@"itemList"]) {
            QYXDailyItemListModel *model = [[QYXDailyItemListModel alloc] initWithDic:d[@"data"]];
            [self.shareDataArr addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

@end
